package LeetCode._03_链表;

import java.util.Stack;

public class _09回文链表 {
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public boolean isPalindrome(ListNode head) {
        if(head == null || head.next == null) return true;
        Stack<Integer> stack = new Stack<>();
        ListNode tmp = head;
        int len = 0;
        while(tmp!=null){
            stack.push(tmp.val);
            tmp = tmp.next;
            len++;
        }
        //只需要比较一半即可
        len >>= 1;
        while(len-- >= 0){
            if(head.val!=stack.pop()) return false;
            head = head.next;
        }
        return true;
    }
}
